package com.nononsenseapps.feeder.mobile.ui.reader.sample

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.billy.android.swipe.SmartSwipe
import com.billy.android.swipe.SmartSwipeWrapper
import com.billy.android.swipe.SwipeConsumer
import com.billy.android.swipe.consumer.BezierBackConsumer
import com.billy.android.swipe.listener.SimpleSwipeListener
import com.leinardi.android.speeddial.SpeedDialActionItem
import com.nononsenseapps.feeder.db.ARG
import com.nononsenseapps.feeder.mobile.R
import com.nononsenseapps.feeder.ui.AbstractFeedActivity
import com.nononsenseapps.feeder.ui.FabClickListener
import com.nononsenseapps.feeder.ui.reader.sample.AbstractReaderFragment
import com.nononsenseapps.feeder.util.TabletUtils
import com.nononsenseapps.feeder.util.bundle
import kotlinx.coroutines.launch
import timber.log.Timber

class ReaderFragment :  AbstractReaderFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                initFab()
            }
        }
    }

    fun initFab(){
        (activity as AbstractFeedActivity).initFab(com.nononsenseapps.feeder.ui.R.drawable.ic_forward_next,com.nononsenseapps.feeder.ui.R.menu.reader_fab,  1,object: FabClickListener {
            override fun fabOnClickListener() {
                moveNext()
            }

            override fun fabOnDoubleClickListener() {
                movePref()
            }

            override fun onActionSelected(actionItem: SpeedDialActionItem?): Boolean {
                if (actionItem != null) {
                    onOptionsItemSelectedWithFab(actionItem.id)
                }
                return false
            }
        })
    }


    override fun getLayoutId(): Int =
        if (TabletUtils.isTablet(activity)) {
            R.layout.fragment_reader_tablet
        } else {
            R.layout.fragment_reader
        }

    override fun buildSwipeView(inView: View): View {
        val swipeRootView = SmartSwipe.wrap(inView)
        if (TabletUtils.isTablet(activity)) {
            swipeRootView.addConsumer(BezierBackConsumer())
                    .enableVertical()
                    .enableNestedFlyAllDirections(false)
                    .setOpenDistance(230)
                    .addListener(object : SimpleSwipeListener() {
                        override fun onSwipeOpened(wrapper: SmartSwipeWrapper?, consumer: SwipeConsumer?, direction: Int) {
                            Timber.d("swipe direction:%d", direction)
                            when (direction) {
                                SwipeConsumer.DIRECTION_TOP -> openInWebView()
                                //SwipeConsumer.DIRECTION_BOTTOM->activity?.onBackPressed()
                            }
                        }
                    })
        }else {
            swipeRootView.addConsumer(BezierBackConsumer())
                    .enableVertical()
                    .enableNestedFlyAllDirections(false)
                    .setOpenDistance(230)
                    .addListener(object : SimpleSwipeListener() {
                        override fun onSwipeOpened(wrapper: SmartSwipeWrapper?, consumer: SwipeConsumer?, direction: Int) {
                            Timber.d("swipe direction:%d", direction)
                            when (direction) {
                                SwipeConsumer.DIRECTION_TOP -> openInWebView()
                                SwipeConsumer.DIRECTION_BOTTOM->if(isSrollBottom()) activity?.onBackPressed()
                            }
                        }
                    })
        }
        mFeedTitleTextView.setOnClickListener {
            getCurrentFeedItem().feedId?.let {
                findNavController().navigate(R.id.action_readerFragment_to_feedFragment, bundle {
                    putLong(ARG.FEED_ID, it)
                })
            }
        }
        return swipeRootView
    }


   override fun openInWebView(){
       getCurrentFeedItem().let { rssItem ->
           rssItem.link?.let { link ->
               findNavController().navigate(
                   R.id.action_readerFragment_to_readerWebViewFragment,
                   bundle {
                       putString(ARG.LINK, link)
                       putString(ARG.ENCLOSURE, rssItem.enclosureLink)
                   }
               )
           }
       }
   }

    override fun navigateToCodeViewFragment() {
        findNavController().navigate(R.id.action_readerFragment_to_codeViewFragment)
    }

    override fun navigateToWebViewFragment(urlLink: String) {
        findNavController().navigate(R.id.action_readerFragment_to_readerWebViewFragment, bundle {
            putString(ARG.LINK, urlLink)
        })
    }
}

